package app.client;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import server.layer.Person;
import server.layer.ServiceProtocol;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: Awake
 * Date: 25.03.12
 * Time: 16:40
 * To change this template use File | Settings | File Templates.
 */
public class RequestHandler {

    public static void receive(String str) {

        try {
            JSONObject fullStr = new JSONObject(str);
            ServiceProtocol command = ServiceProtocol.getCommand(fullStr.getString("command"));

            switch (command) {
                case UPDATE_LIST:
                    receiveList(fullStr);
                    break;
                case UPDATE_IMAGE:
                    receiveImage(fullStr);
                    break;
            }

        } catch (JSONException e1) {
            e1.printStackTrace();
        }
    }

    public static void receiveList(JSONObject fullStr) {
        MyModel.personList.clear();
        try {
            JSONArray jArray = fullStr.getJSONArray("array");
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject object = jArray.getJSONObject(i);
                Person person = getPerson(object);
                MyModel.personList.add(person);
                ViewPanel.tableModel.fireTableDataChanged();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static void receiveImage(JSONObject object) {
        try {
            JSONArray jImg = object.getJSONArray("img");
            byte[] img = new byte[jImg.length()];
            for (int i = 0; i < jImg.length(); i++) {
                img[i] = (byte) jImg.getInt(i);
            }
            InputStream in = new ByteArrayInputStream(img);
            BufferedImage image = ImageIO.read(in);
            ViewPanel.imagePanel.setImage(image);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static void receivePhones(JSONObject object) {

    }

    public static void receiveAddress(JSONObject object) {

    }

    public static Person getPerson(JSONObject object) {
        Person person = new Person();
        {
            try {
                if (object.has("person_id")) {
                    person.setId(object.getInt("person_id"));
                }
                if (object.has("lName")) {
                    person.setFirstName(object.getString("fName"));
                }
                if (object.has("lName")) {
                    person.setLastName(object.getString("lName"));
                }
                if (object.has("birthDate")) {
                    Long time = object.getLong("birthDate");
                    Date date = new Date(time);
                    person.setBirthDate(date);
                }
                if (object.has("eMail")) {
                    person.seteMail(object.getString("eMail"));
                }
                if (object.has("description")) {
                    person.setDescription(object.getString("description"));
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return person;
    }

}
